import java.io.*;
import java.util.*;

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class ContactPage extends JPanel
{
	File temp = new File ("contacts");
	String path = temp.getAbsolutePath();

	private JPanel manageP;
	private JLabel infoString, modifyString, deleteString;
	private JButton infoB, modifyB, deleteB, searchB, backB, signoutB;
	private JTextField searchTF;

	private ContactList ctlist;
	private JScrollPane scroll;

	private JPanel createP, createP1, createP2, createP3, createP4, createP5;
	private JLabel f_nameString, l_nameString, companyString, departmentString, positionString, mobileString, phoneString, faxString, addressString, emailString, homepageString;
	private JTextField f_nameTF, l_nameTF, companyTF, departmentTF, positionTF, mobileTF, phoneTF, faxTF, addressTF, emailTF, homepageTF;
	private JButton createB;

	private int id_checked = 2, residence_checked = 2;
	private int pw_digit = 1, residence_digit = 1;

	private boolean getUnaccessed = false;



	public ContactPage ()
	{
		setBackground (Color.white);
		setLayout (new BorderLayout ());


		manageP = new JPanel();
			manageP.setBackground (new Color (252,245,245));
			manageP.setBorder (BorderFactory.createLineBorder (Color.white, 7));

		infoString = new JLabel ("view more");
		infoB = new JButton (new ImageIcon ("info.gif"));
			infoB.setBackground (new Color (252,245,245));
			infoB.setBorder (BorderFactory.createLineBorder (new Color (252,245,245), 1));
			infoB.addActionListener (new EventsListener());


		modifyString = new JLabel ("modify");	
		modifyB = new JButton (new ImageIcon ("modify.gif"));
			modifyB.setBackground (new Color (252,245,245));
			modifyB.setBorder (BorderFactory.createLineBorder (new Color (252,245,245), 1));
			modifyB.addActionListener (new EventsListener());
		

		deleteString = new JLabel ("delete");
		deleteB = new JButton (new ImageIcon ("delete.gif"));
			deleteB.setBackground (new Color (252,245,245));
			deleteB.setBorder (BorderFactory.createLineBorder (new Color (252,245,245), 1));
			deleteB.addActionListener (new EventsListener());


		searchTF = new JTextField (10);
		searchB = new JButton (new ImageIcon ("search.gif"));
			searchB.setBackground (new Color (252,245,245));
			searchB.setBorder (BorderFactory.createLineBorder (new Color (252,245,245), 1));
			searchB.addActionListener (new EventsListener());
		backB = new JButton ("BACK");
			backB.setBackground (new Color (255,238,238));
			backB.setBorder (BorderFactory.createLineBorder (Color.white, 6));
			backB.addActionListener (new EventsListener());

		signoutB = new JButton ("Sign Out", new ImageIcon("out.gif"));
			signoutB.setBackground (new Color (255,238,238));
			signoutB.setBorder (BorderFactory.createLineBorder (Color.white, 6));
			signoutB.addActionListener (new EventsListener());



		manageP.add (infoString);
		manageP.add (infoB);
		manageP.add (Box.createRigidArea (new Dimension (10,0)));
		manageP.add (Box.createHorizontalGlue());
		manageP.add (modifyString);
		manageP.add (modifyB);
		manageP.add (Box.createRigidArea (new Dimension (10,0)));
		manageP.add (Box.createHorizontalGlue());
		manageP.add (deleteString);
		manageP.add (deleteB);
		manageP.add (Box.createRigidArea (new Dimension (30,0)));
		manageP.add (Box.createHorizontalGlue());
		manageP.add (searchTF);
		manageP.add (searchB);
		manageP.add (backB);
		manageP.add (Box.createRigidArea (new Dimension (30,0)));
		manageP.add (Box.createHorizontalGlue());
		manageP.add (signoutB);





		
		ctlist = new ContactList ();
		scroll = new JScrollPane (ctlist);
			scroll.setPreferredSize (new Dimension (702,235));
			scroll.setBorder (BorderFactory.createLineBorder (Color.white, 7));







		createP = new JPanel ();
			createP.setLayout (new GridLayout (5,1));
			createP.setBorder (BorderFactory.createLineBorder (Color.white,7));
		


		createP1 = new JPanel ();
			createP1.setBackground (new Color (252,245,245));

		createB = new JButton ("REGISTER", new ImageIcon ("create.gif"));
			createB.setBackground (new Color (255,238,238));
			createB.setBorder (BorderFactory.createLineBorder (Color.white, 6));
			createB.addActionListener (new EventsListener());

		f_nameString = new JLabel ("First Name : ");
		f_nameTF = new JTextField (10);

		l_nameString = new JLabel ("Last Name : ");
		l_nameTF = new JTextField (10);

		createP1.add (f_nameString);
		createP1.add (f_nameTF);
		createP1.add (Box.createRigidArea (new Dimension (15,0)));
		createP1.add (Box.createHorizontalGlue());
		createP1.add (l_nameString);
		createP1.add (l_nameTF);
		createP1.add (Box.createRigidArea (new Dimension (40,0)));
		createP1.add (Box.createHorizontalGlue());
		createP1.add (createB);



		createP2 = new JPanel ();
			createP2.setBackground (new Color (252,245,245));

		companyString = new JLabel ("Company : ");
		companyTF = new JTextField (8);

		departmentString = new JLabel ("Department : ");
		departmentTF = new JTextField (8);
		
		positionString = new JLabel ("Position : ");
		positionTF = new JTextField (8);

		createP2.add (companyString);
		createP2.add (companyTF);
		createP2.add (Box.createRigidArea (new Dimension (25,0)));
		createP2.add (Box.createHorizontalGlue());
		createP2.add (departmentString);
		createP2.add (departmentTF);
		createP2.add (Box.createRigidArea (new Dimension (25,0)));
		createP2.add (Box.createHorizontalGlue());
		createP2.add (positionString);
		createP2.add (positionTF);



		createP3 = new JPanel ();
			createP3.setBackground (new Color (252,245,245));

		mobileString = new JLabel ("Mobile : ");
		mobileTF = new JTextField (8);

		phoneString = new JLabel ("Phone : ");
		phoneTF = new JTextField (8);

		faxString = new JLabel ("Fax : ");
		faxTF = new JTextField (8);

		createP3.add (mobileString);
		createP3.add (mobileTF);
		createP3.add (Box.createRigidArea (new Dimension (10,0)));
		createP3.add (Box.createHorizontalGlue());
		createP3.add (phoneString);
		createP3.add (phoneTF);
		createP3.add (Box.createRigidArea (new Dimension (10,0)));
		createP3.add (Box.createHorizontalGlue());
		createP3.add (faxString);
		createP3.add (faxTF);



		createP4 = new JPanel ();
			createP4.setBackground (new Color (252,245,245));

		addressString = new JLabel ("Address : ");
		addressTF = new JTextField (30);

		createP4.add (addressString);
		createP4.add (addressTF);
		createP4.add (Box.createRigidArea (new Dimension (25,0)));
		createP4.add (Box.createHorizontalGlue());



		createP5 = new JPanel ();
			createP5.setBackground (new Color (252,245,245));

		emailString = new JLabel ("E-Mail : ");
		emailTF = new JTextField (15);

		homepageString = new JLabel ("Homepage : ");
		homepageTF = new JTextField (15);

		createP5.add (emailString);
		createP5.add (emailTF);
		createP5.add (Box.createRigidArea (new Dimension (25,0)));
		createP5.add (Box.createHorizontalGlue());
		createP5.add (homepageString);
		createP5.add (homepageTF);




		createP.add (createP1);
		createP.add (createP2);
		createP.add (createP3);
		createP.add (createP4);
		createP.add (createP5);




		add (manageP, BorderLayout.NORTH);
		add (scroll, BorderLayout.CENTER);
		add (createP, BorderLayout.SOUTH);
		
	}



	private class EventsListener implements ActionListener
	{
		int modifyMB, deleteMB, createMB, signoutMB;
		String pw_modifyMB, address_modifyMB;

		public void actionPerformed (ActionEvent event)
		{

			if (event.getSource() == infoB)
			{
				JOptionPane.showMessageDialog (null, "First Name : " + ctlist.getFName (ctlist.selected)
																		+ "\nLast Name : " + ctlist.getLName (ctlist.selected)
																		+ "\n\nCompany : " + ctlist.getCompany (ctlist.selected)
																		+ "\nDepartmet : " + ctlist.getDepartment (ctlist.selected)
																		+ "\nPosition : " + ctlist.getPosition (ctlist.selected)
																		+ "\n\nMobile : " + ctlist.getMobile (ctlist.selected)
																		+ "\nPhone : " + ctlist.getPhone (ctlist.selected)
																		+ "\nFax : " + ctlist.getFax (ctlist.selected)
																		+ "\n\nEmail : " + ctlist.getEmail (ctlist.selected)
																		+ "\nAddress : " + ctlist.getAddress (ctlist.selected)
																		+ "\nHomepage : " + ctlist.getHomepage (ctlist.selected) + ctlist.getAddress (ctlist.selected) + "\n");
			}



			if (event.getSource() == modifyB)
			{
				// deleteFile
				// createFile
				// ctlist.callRecentInfo();
			}



			if (event.getSource() == deleteB)
			{
				deleteMB = JOptionPane.showConfirmDialog (null, "Do you really want to delete the contact?");

				if (deleteMB == JOptionPane.YES_OPTION)
				{
					if (ctlist.selected != -1)
					{
						if (deleteFile (ctlist.getFName(ctlist.selected), ctlist.getLName(ctlist.selected), ctlist.getMobile(ctlist.selected), ctlist.getPhone(ctlist.selected), ctlist.getFax(ctlist.selected)))
							ctlist.deleteContact (ctlist.selected);
						else
							JOptionPane.showMessageDialog (null, "Error occurred.");
					}
					else	JOptionPane.showMessageDialog (null, "No contact selected to be deleted.");
				}
			}



			if (event.getSource() == createB)
			{
				/*String tempPW = pwTF1.getText() + pwTF2.getText() + pwTF3.getText() + pwTF4.getText();
				pw_digit = 1;

				if ((pwTF1.getText()).length() == 0 || (pwTF2.getText()).length() == 0 || (pwTF3.getText()).length() == 0 || (pwTF4.getText()).length() == 0
					|| (nameTF1.getText()).length() == 0 || (nameTF2.getText()).length() == 0 || (addressTF.getText()).length() == 0)
					JOptionPane.showMessageDialog (null, "You have to fill the form up completely.");

				else if ((pwTF1.getText()).length() != 1 || (pwTF2.getText()).length() != 1 || (pwTF3.getText()).length() != 1 || (pwTF4.getText()).length() != 1 || !allDigits(tempPW))
					JOptionPane.showMessageDialog (null, "You can put only ONE digit in each PassWord input boxes.");

				else if (id_checked == 0 || id_checked == 2 || residence_checked == 0 || residence_checked == 2)
					JOptionPane.showMessageDialog (null, "You must check your ID and residence number are not existing present.");

				else if (!allCharacters(nameTF1.getText()) || !allCharacters(nameTF2.getText()))
					JOptionPane.showMessageDialog (null, "You cannot put digits in Name input boxes.");

				else
				{*/
					createMB = JOptionPane.showConfirmDialog (null, "Do you really want to create the contact?");

					if (createMB == JOptionPane.YES_OPTION)
					{
						String f_name = f_nameTF.getText();
						String l_name = l_nameTF.getText();
						String company = companyTF.getText();
						String department = departmentTF.getText();
						String position = positionTF.getText();
						String mobile = mobileTF.getText();
						String phone = phoneTF.getText();
						String fax = faxTF.getText();
						String address = addressTF.getText();
						String email = emailTF.getText();
						String homepage = homepageTF.getText();

						if (makeFile (f_name, l_name, company, department, position, mobile, phone, fax, address, email, homepage))
						{
							ctlist.createContact (f_name, l_name, company, department, position, mobile, phone, fax, address, email, homepage);
							textfield_reset();
						}
						else
							JOptionPane.showMessageDialog (null, "Error occurred.");
					}
				//}
			}



			if (event.getSource() == searchB)
			{

			}


	
			if (event.getSource() == backB)
			{

			}



			if (event.getSource() == signoutB)
			{
				signoutMB = JOptionPane.showConfirmDialog (null, "Do you really want to sign out?");

				if (signoutMB == JOptionPane.YES_OPTION)
				{
					getUnaccessed = true;
					textfield_reset();
				}
			}
		}
	}



	public boolean makeFile (String f_name, String l_name, String company, String department, String position, String mobile, String phone, String fax, String address, String email, String homepage)
	{
		boolean result = true;

		try
		{
			String filename = f_name + "&" + l_name + "&" + mobile + "&" + phone + "&" + fax + ".txt";
			File file = new File (path, filename);

			FileWriter fw= new FileWriter (file);
			BufferedWriter bw = new BufferedWriter (fw);
			PrintWriter pw = new PrintWriter (bw);

			pw.print (f_name + "&&&&" + l_name + "&&&&" + company + "&&&&" + department + "&&&&" + position + "&&&&" + mobile + "&&&&" + phone + "&&&&" + fax + "&&&&" + address + "&&&&" + email + "&&&&" + homepage);
			pw.close ();
		}
		catch (IOException ioe)		{	result = false;	}

		return result;
	}


	public boolean deleteFile (String f_name, String l_name, String mobile, String phone, String fax)
	{
		boolean result = true;

		String filename = f_name + "&" + l_name + "&" + mobile + "&" + phone + "&" + fax + ".txt";
		File file = new File (path, filename);

		if (file.delete())	 ;
		else result = false;

		return result;
	}
	
	private void textfield_reset ()
	{
		searchTF.setText ("");
		
		f_nameTF.setText ("");
		l_nameTF.setText ("");
		companyTF.setText ("");
		departmentTF.setText ("");
		positionTF.setText ("");
		mobileTF.setText ("");
		phoneTF.setText ("");
		faxTF.setText ("");
		addressTF.setText ("");
		emailTF.setText ("");
		homepageTF.setText ("");
	}

	public void callRecentInfo ()	 {	ctlist.callRecentInfo();	}

	public boolean getGetUnaccessed ()	 {	return getUnaccessed;	}
	public void resetGetUnaccessed ()	{	getUnaccessed = false;	}

}